package com.mng.index12306.framework.starter.bases;


import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Singleton {

    private static final ConcurrentHashMap<String, Object> SINGLE_OBJECT_POOL = new ConcurrentHashMap<>();

    /**
     * 根据 Key 获取单例对象
     */
    public static <T> T get(String key){
        Object result = SINGLE_OBJECT_POOL.get(key);
        return result == null?null:(T) result;
    }

    /**
     * 根据 Key 获取单例对象
     * <p> 为空时，通过supplier 构建到单例对象并放入容器
     */
    public static <T> T get(String key, Supplier<T> supplier){
        Object result = SINGLE_OBJECT_POOL.get(key);
        if(result == null && (result = supplier.get())!=null){
            SINGLE_OBJECT_POOL.put(key,result);
        }
        return result == null?null:(T)result;
    }
    /**
     * 对象放入容器
     */
    public static void put(Object value) {
        put(value.getClass().getName(), value);
    }

    /**
     * 对象放入容器
     */
    public static void put(String key, Object value) {
        SINGLE_OBJECT_POOL.put(key, value);
    }

}
